using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Irc;
using Irc.Events;
using Irc.Objects;

namespace Irc.Net
{
	public partial class ChannelTab : UserControl
	{
		private Channel channelObject;
		public Channel Target
		{
			get { return channelObject; }
		}
		public ChannelTab(Channel channel)
		{
			InitializeComponent();
			channelObject = channel;
			CommandText.Name = Name + "-" + CommandText.Name;
			CommandText.KeyDown += Main.ParseCommand;
			Name = channel.Name;
			Dock = DockStyle.Fill;
			if(channel.Topic != null)
			{
				Topic.Text = channel.Topic.Text;
				Irc.Objects.Message m = new Irc.Objects.Message();
				m.Text = String.Format(Strings.TopicChangeText, channel.Topic.Text, channel.Topic.Sender, channel.Topic.Time.ToString(SettingsManager.Instance["TimestampFormat"]));
				AddMessage(m);
			}
			#region event handlers
			channel.Join += delegate(object sender, JoinEventArgs e) {
					if(FindForm() == null)
						return;
					if(!e.Movement.User.Equals(new Nickname(((Network)FindForm()).Client.Nickname)))
					{
						Irc.Objects.Message m = new Irc.Objects.Message();
						m.Text = String.Format((e.Movement is UserJoin ? Strings.ChannelJoinText : Strings.ChannelPartText), e.Movement.User, e.Movement.Target);
						AddMessage(m);
					}
					if(e.Movement is UserJoin)
						Nicknames.BeginInvoke((MethodInvoker)delegate {if(!Nicknames.Items.Contains(e.Movement.User))Nicknames.Items.Add(e.Movement.User);});
					else if(e.Movement is UserPart)
						Nicknames.BeginInvoke((MethodInvoker)delegate {if(Nicknames.Items.Contains(e.Movement.User))Nicknames.Items.Remove(e.Movement.User);});
				};
			channel.Nicklist += delegate(object sender, NicklistEventArgs e) {
					Nicknames.BeginInvoke((MethodInvoker)delegate {
						foreach(Nickname item in e.Nicklist.Nicknames)
							if(!Nicknames.Items.Contains(item))
								Nicknames.Items.Add(item);
					});
				};
			channel.PublicMessage += delegate(object sender, MessageEventArgs e) {
					AddMessage(e.Message);
				};
			channel.PublicNotice += delegate(object sender, MessageEventArgs e) {
					AddMessage(e.Message);
				};
			channel.PublicAction += delegate(object sender, MessageEventArgs e) {
					AddMessage(e.Message);
				};
			channel.NickChange += delegate(object sender, NickEventArgs e) {
					Irc.Objects.Message m = new Irc.Objects.Message();
					m.Text = String.Format(Strings.NickChangeText, e.OldNick, e.NewNick);
					AddMessage(m);
				};
			channel.TopicChange += delegate(object sender, TopicEventArgs e) {
					Topic.Invoke((MethodInvoker)delegate{Topic.Text = e.Topic.Text;});
					Irc.Objects.Message m = new Irc.Objects.Message();
					m.Text = String.Format(Strings.TopicChangeText, e.Topic.Text, e.Topic.Sender, e.Topic.Time.ToString(SettingsManager.Instance["TimestampFormat"]));
					AddMessage(m);
				};
			#endregion
		}

		public void AddMessage(Irc.Objects.Message message)
		{
			if(ChannelText.Created)
				ChannelText.Invoke((MethodInvoker)delegate{
					ChannelText.AppendMessage(message);
				});
			Refocus();
		}
		private void ChangeTopic(object sender, KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				channelObject.ChangeTopic(Topic.Text);
			}
		}
		public void Refocus()
		{
			if(CommandText.Created)
				CommandText.BeginInvoke((MethodInvoker)delegate { CommandText.Focus(); });
		}
		public void PartChannel()
		{
			channelObject.Part();
		}
	}
}
